package org.radigan.ycoa.test.service

import org.radigan.ycoa.scan.service.ScanService

import org.osgi.framework.BundleActivator
import org.osgi.framework.BundleContext
import org.osgi.util.tracker.ServiceTracker

import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory

public class TestService implements BundleActivator {

  private static final transient Log log = LogFactory.getLog(TestService.class)

  private ServiceTracker scanServiceTracker
  private ScanService scanService

  public void setService(ScanService scanService) {
    this.scanService = scanService
  }

  public void removeService() {
    this.scanService = null
  }

  public void start(BundleContext context) {
    try {
      log.info("starting...")
      scanServiceTracker = new ServiceTracker(context, ScanService.class.getName(), null)
      scanServiceTracker.open()
      ScanService scanService = (ScanService) scanServiceTracker.getService()
      if(scanService==null) {
        println "Scan service unavailable on TestService start"
      } else {
        scanService.scan()
      }
    } catch(Exception e) {
      e.printStackTrace()
    }
  }

  public void stop(BundleContext context) {
    try {
      log.info("stopping...")
      ScanService scanService = (ScanService) scanServiceTracker.getService()
      if (scanService==null) {
        println "Scan service unavailable on TestService stop"
      } else {
      }
      scanServiceTracker.close()
    } catch(Exception e) {
      e.printStackTrace()
    }
  }

}
